package com.eian.boot.module.system.log.service;

import com.eian.boot.common.core.utils.BeanCopierUtils;
import com.eian.boot.module.system.log.mapper.SysOperationLogMapper;
import com.eian.boot.module.system.log.model.dto.OperationLogQueryRequest;
import com.eian.boot.module.system.log.model.entity.SysOperationLogEntity;
import com.eian.boot.module.system.log.model.vo.OperationLogDetailVO;
import com.eian.boot.module.system.log.model.vo.OperationLogVO;
import com.eian.boot.mybatis.core.page.PageResult;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryChain;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 系统操作日志服务
 *
 * @author alex.meng
 */
@Slf4j
@Service
public class SysOperationLogService extends com.eian.boot.mybatis.core.service.BaseService<SysOperationLogMapper, SysOperationLogEntity> {

    /**
     * 异步保存操作日志
     */
    @Async
    public void saveOperationLog(SysOperationLogEntity logEntity) {
        try {
            this.save(logEntity);
        } catch (Exception e) {
            log.error("保存操作日志失败", e);
        }
    }

    /**
     * 分页查询操作日志
     * 应用数据权限控制，基于操作人所在部门过滤
     */
    @com.eian.boot.common.annotation.DataScope(deptAlias = "t_sys_operation_log")
    public PageResult<OperationLogVO> queryOperationLogs(OperationLogQueryRequest request) {
        QueryChain<SysOperationLogEntity> queryChain = this.queryChain();

        if (StringUtils.isNotBlank(request.getModule())) {
            queryChain.like(SysOperationLogEntity::getModule, request.getModule());
        }
        if (StringUtils.isNotBlank(request.getOperatorName())) {
            queryChain.like(SysOperationLogEntity::getOperatorName, request.getOperatorName());
        }
        if (request.getStatus() != null) {
            queryChain.eq(SysOperationLogEntity::getStatus, request.getStatus());
        }
        if (request.getBusinessType() != null) {
            queryChain.eq(SysOperationLogEntity::getBusinessType, request.getBusinessType());
        }
        if (request.getStartTime() != null) {
            queryChain.ge(SysOperationLogEntity::getCreatedAt, request.getStartTime());
        }
        if (request.getEndTime() != null) {
            queryChain.le(SysOperationLogEntity::getCreatedAt, request.getEndTime());
        }
        queryChain.orderBy(SysOperationLogEntity::getCreatedAt, false);

        Page<SysOperationLogEntity> page = queryChain.page(
                Page.of(request.getCurrent(), request.getPageSize())
        );

        List<OperationLogVO> voList = BeanCopierUtils.copyList(page.getRecords(), OperationLogVO.class);
        return PageResult.build(voList, page.getTotalRow());
    }

    /**
     * 获取日志详情
     */
    public OperationLogDetailVO getOperationLogDetail(Long id) {
        SysOperationLogEntity entity = this.getById(id);
        return BeanCopierUtils.copy(entity, OperationLogDetailVO.class);
    }

    /**
     * 批量删除历史日志（物理删除）
     */
    public void deleteOperationLogs(List<Long> ids) {
        if (ids != null && !ids.isEmpty()) {
            this.removeByIds(ids);
        }
    }
}

